classSolution { public: intminimumCost(vector<int>& cost){ sort(cost.begin(), cost.end(), greater<int>()); int ans = 0, n = cost.size(); for (int i = 0; i < n; i += 3) { ans += cost[i]; if (i + 1 < n) ans += cost[i + 1]; } return ans; } };
using LL = longlong; classSolution { public: intnumberOfArrays(vector<int>& d, int L, int R){ int n = d.size(); vector<LL> sb(n); sb[0] = d[0]; for (int i = 1; i < n; i ++ ) sb[i] = sb[i - 1] + d[i]; LL Mx = *max_element(begin(sb), end(sb)), Mn = *min_element(begin(sb), end(sb)); int ans = 0; for (int i = L; i <= R; i ++ ) { LL curL = Mn + i, curR = Mx + i; if (curL >= L and curL <= R and curR >= L and curR <= R) ans += 1; } return ans; } };
using PII = pair<int, int>; using TII = tuple<int, int, int, int>; constint dx[4] = {0, 0, 1, -1}, dy[4] = {1, -1, 0, 0}; constint INF = 1e9; classSolution { public: vector<vector<int>> highestRankedKItems(vector<vector<int>>& g, vector<int>& p, vector<int>& start, int k) { int L = p[0], R = p[1]; int n = g.size(), m = g[0].size();
int sx = start[0], sy = start[1]; vector<vector<int>> dist(n, vector<int>(m, INF)); dist[sx][sy] = 0; queue<PII> qu; qu.emplace(sx, sy); while (qu.size()) { auto [x, y] = qu.front(); qu.pop(); for (int i = 0; i < 4; i ++ ) { int nx = x + dx[i], ny = y + dy[i]; if (nx >= 0and nx < n and ny >= 0and ny < m and g[nx][ny]) { if (dist[nx][ny] == INF) { dist[nx][ny] = dist[x][y] + 1; qu.emplace(nx, ny); } } } } set<TII> st; for (int i = 0; i < n; i ++ ) for (int j = 0; j < m; j ++ ) { if (dist[i][j] == INF or g[i][j] > R or g[i][j] < L or g[i][j] == 1) continue; st.emplace(dist[i][j], g[i][j], i, j); } vector<vector<int>> ans; for (auto& [d, p, i, j] : st) { if (ans.size() < k) ans.push_back({i, j}); else break; } return ans; } };